package cst.a931xmc.yuejian20.Activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import cst.a931xmc.yuejian20.Bean.TrainBean;
import cst.a931xmc.yuejian20.Listener.Listener;
import cst.a931xmc.yuejian20.Model.ApiModel;
import cst.a931xmc.yuejian20.Model.TrainModel;
import cst.a931xmc.yuejian20.R;

public class TrainActivity extends AppCompatActivity implements View.OnClickListener {
    private Context context;
    private int train_id;
    private ImageView train_pic;
    private TextView train_name;
    private TextView train_type;
    private TextView train_place;
    private TextView train_time;
    private TextView train_tel;
    private TextView train_appnum;
    private TextView train_price;
    private TextView train_intro;
    private TextView train_notice;
    private ImageButton btn_back;
    private Button btn_app;
    private TrainModel model;
    private Listener<TrainBean> listener;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_train);
        init();
        initData();
        model = new TrainModel();
        model.getDetail(train_id,listener);
    }

    public void init(){
        train_pic = findViewById(R.id.train_pic);
        train_name = findViewById(R.id.train_name);
        train_type = findViewById(R.id.train_type);
        train_place = findViewById(R.id.train_place);
        train_time = findViewById(R.id.train_time);
        train_tel = findViewById(R.id.train_tel);
        train_intro = findViewById(R.id.train_intro);
        train_price = findViewById(R.id.train_price);
        train_appnum = findViewById(R.id.train_appnum);
        train_notice = findViewById(R.id.train_notice);
        btn_back = findViewById(R.id.btn_back);
        btn_app = findViewById(R.id.btn_app);
        btn_back.setOnClickListener(this);
        btn_app.setOnClickListener(this);
        Intent intent = getIntent();
        train_id = intent.getExtras().getInt("train_id");
    }

    public void initData(){
        listener = new Listener<TrainBean>() {
            @Override
            public void onResponse(TrainBean l) {
                if(l.getTrain_id()!=0){
                    train_name.setText(l.getTrain_name());
                    train_place.setText("地点：" + l.getTrain_place());
                    train_time.setText("时间：" + l.getTrain_time());
                    train_tel.setText("咨询电话：" + l.getTrain_tel());
                    train_appnum.setText(l.getTrain_appnum());
                    train_price.setText("￥" + l.getTrain_price() + "/人次");
                    if(l.getTrain_type()==1)
                        train_type.setText("入门级");
                    else if(l.getTrain_type()==2)
                        train_type.setText("菜鸟级");
                    else if(l.getTrain_type()==3)
                        train_type.setText("进阶级");
                    train_intro.setText(l.getTrain_intro());
                    train_notice.setText(l.getTrain_notice());
                    ApiModel apiModel = new ApiModel();
                    Picasso.with(context)
                            .load(apiModel.getBaseUrl() + "static/images/" + l.getTrain_pic())
                            .placeholder(R.drawable.nopic)
                            .into(train_pic);
                }
                else {
                    Toast.makeText(TrainActivity.this,"加载失败",Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFail(String message) {
                Toast.makeText(TrainActivity.this,message, Toast.LENGTH_LONG).show();
            }
        };
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_app:
                Intent intent = new Intent(TrainActivity.this, TrainAppActivity.class);
                intent.putExtra("train_id", train_id);
                startActivity(intent);
                finish();
                break;
        }
    }
}
